﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WSBanking
{
	#region Using
	using WSBanking.DAO;
	using WSBanking.DTO;
	using WSBanking.Helpers;
	using System.Data;
	using System.IO;
	#endregion

	/// <summary>
	/// The class is used to interact with database
	/// </summary>
	public class ServiceBanking: IServiceBanking
	{
        public SEBanking_AccountDTO CheckAccount(string UserID, string Password)
        {
            var rpt = new SEBanking_AccountDAO(GlobalVariables.DbConnectionString);
            var ds = rpt.SelectByUserIDAndPassword(UserID, Password);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                return SEBanking_AccountDTO.Translate(ds.Tables[0].Rows[0]);
            }
            return null;
        }

        public bool CheckCardID(string idCard)
        {
            var rpt = new SEBanking_CardDAO(GlobalVariables.DbConnectionString);
            var ds = rpt.SelectByID(idCard);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }


        public decimal GetBalance(string idCard)
        {
            var rpt = new SEBanking_CardDAO(GlobalVariables.DbConnectionString);
            var ds = rpt.SelectByID(idCard);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                return SEBanking_CardDTO.Translate(ds.Tables[0].Rows[0]).Card_Banlance;
            }
            return -1;
        }

        public int ExChangeVND(string idCardSource, string SecurityCode, string idCardTarget, decimal money)
        {
            if (money <= 0)
            {
                return 0;
            }

            // Get Cart Source
            var rpt = new SEBanking_CardDAO(GlobalVariables.DbConnectionString);
            var ds = rpt.SelectByID(idCardSource);
            if (ds == null || (ds != null && ds.Tables[0].Rows.Count == 0))
            {
                return -1;
            }
            var cartSource = SEBanking_CardDTO.Translate(ds.Tables[0].Rows[0]);

            if (SecurityCode != cartSource.Card_SecurityCode)
            {
                return -3;
            }
            if ((decimal)cartSource.Card_Banlance < money)
            {
                return -2;
            }

            // Get Cart Target
            rpt = new SEBanking_CardDAO(GlobalVariables.DbConnectionString);
            ds = rpt.SelectByID(idCardTarget);
            if (ds == null || (ds != null && ds.Tables[0].Rows.Count == 0))
            {
                return -4;
            }
            var cartTarget = SEBanking_CardDTO.Translate(ds.Tables[0].Rows[0]);

            decimal b = (decimal)cartSource.Card_Banlance - money;
            cartSource.Card_Banlance = b;
            cartTarget.Card_Banlance += money;

            try
            {
                // Update Cart Source
                rpt = new SEBanking_CardDAO(GlobalVariables.DbConnectionString);
                rpt.Update(cartSource);

                // Update Cart Target
                rpt = new SEBanking_CardDAO(GlobalVariables.DbConnectionString);
                rpt.Update(cartTarget);

                // Update banking exchange
                var ex = new SEBanking_ExchangeDTO()
                {
                    Card_ID_Send = idCardSource,
                    Card_ID_Receive = idCardTarget,
                    Exc_Balance = money
                };
                var rpt2 = new SEBanking_ExchangeDAO(GlobalVariables.DbConnectionString);
                rpt2.Insert(ex);
            }
            catch
            {
                return -5;
            }
            return 1;
        }
    }
}
